sampler2D g_samSrcColor;

texture baseTex;
texture selTex;
texture shadowTex;
texture holeTex;


sampler Sampler0 = sampler_state {
	texture = <baseTex>; 
	magfilter = LINEAR;
	minfilter = LINEAR;
	mipfilter=LINEAR;
	AddressU = wrap;
	AddressV = wrap;
};

sampler selSamp = sampler_state {
	texture = <selTex>;
	magfilter = LINEAR;
	minfilter = LINEAR;
	mipfilter = LINEAR;
	AddressU = wrap;
	AddressV = wrap;
};

sampler shadowSamp = sampler_state {
	texture = <shadowTex>;
	magfilter = LINEAR;
	minfilter = LINEAR;
	mipfilter = LINEAR;
	AddressU = wrap;
	AddressV = wrap;
};

sampler holeSamp = sampler_state {
	texture = <holeTex>;
	magfilter = LINEAR;
	minfilter = LINEAR;
	mipfilter = LINEAR;
	AddressU = wrap;
	AddressV = wrap;
};

float radius; // radius of the selection circle
float3 mouse; // the point that the mouse is hovering in world-coordinates
float3 click; // the point where we clicked on in world-coordinates
bool drawSel; // if we should draw the circle around click
bool texEnable; // Is texturing enabled? Used to determine if we can draw a square-brush
float brushType; // The type of the brush (circle, square)
float sqrt2; // Squareroot of 2 (increases performance to be set as a global value)
bool drawHL; // Heightlines on/off
float hlDist; // distance between two heightlines
float3 SunDirection = float3(-1, -1, -1);

float4x4 viewProj;

struct PixelInput
{
	float4 pos : POSITION0;
	float2 Tex : TEXCOORD0;
	float2 Tex2 : TEXCOORD1;
	float3 Tex3 : TEXCOORD2;
	float4 TexClr : TEXCOORD3;
	float3 Normal : TEXCOORD4;
};

struct VertexInput
{
	float4 pos : POSITION0;
	float4 clr : COLOR0;
	float2 tex1 : TEXCOORD0;
	float2 tex2 : TEXCOORD1;
	float3 normal : NORMAL;
};

float4 ApplySunLight(float4 base, float3 normal)
{
	float light = dot(normal, -normalize(SunDirection));
	if(light < 0)
		light = 0;
	if(light > 0.5f)
		light = 0.5f + (light - 0.5f) * 0.65f;
		
	base.rgb *= light + 0.5f;
	return base;
}

PixelInput VShader(VertexInput input)
{
	PixelInput ret = (PixelInput)0;
	ret.pos = mul(float4(input.pos.xyz, 1.0f), viewProj);
	ret.Tex = input.tex1;
	ret.Tex2 = input.tex2;
	ret.Tex3 = input.pos.xyz;
	ret.TexClr = input.clr;
	ret.Normal = normalize(input.normal);
	return ret;
}

float4 MyShader(PixelInput pInput) : COLOR0
{
 // float4 retu = float4(0, 0, 0, 1);
  //retu.r = (pInput.Tex3.y % 1024) / 1024;
  //return retu;
	float4 Color;
	float4 black = float4(0, 0, 0, 1);
	Color = tex2D(Sampler0, pInput.Tex.xy);
	float4 shadow = tex2D(shadowSamp, pInput.Tex2.xy);
	float4 hole = tex2D(holeSamp, pInput.Tex2.xy);

	float4 color = (Color * pInput.TexClr * 2);
	float4 ret = shadow.a * black + (1 - shadow.a) * color;
	ret = ApplySunLight(ret, pInput.Normal);
	ret *= hole.a;

	float dx = pInput.Tex3.x - mouse.x;
	float dz = pInput.Tex3.z - mouse.z;

	float tot = sqrt(dx * dx + dz * dz);
	if((abs(dx) < radius && abs(dz) < radius && brushType == 1) || (tot <= radius && brushType == 0) || (tot <= radius && !texEnable))
	{
		//float2 texco = float2(dx / (radius * 2) + 0.5f, dz / (radius * 2) + 0.5f);
		float4 white;
		
		//if(brushType == 0 || !texEnable)
		//	white = tex2D(selSamp, texco.xy);
		//else
			white.a = 0.3;
			
		//white.r = 1.0f - ret.r;
		//white.g = 1.0f - ret.g;
		//white.b = 1.0f - ret.b;
		
		white.r = white.g = white.b = 0.5f;
      
        
		ret = white.a * white + (1.0f - white.a) * ret;
		
		if(abs(pInput.Tex3.x) % 33.333333125 < 0.1 || abs(pInput.Tex3.z) % 33.333333125 < 0.1)
			ret = float4(1, 0, 0, 1);
			
		if(abs(pInput.Tex3.y) % hlDist < 0.5 && drawHL)
		{
			ret.a = 1;
			ret.r = ret.g = ret.b = 0;
		}
		
		return ret;
	}
    
	if(drawSel)
	{
		dx = pInput.Tex3.x - click.x;
		dz = pInput.Tex3.z - click.z;
		tot = sqrt(dx * dx + dz * dz);
		if((abs(dx) < 5 && abs(dz) < 5 && brushType == 1 && texEnable == true) || (tot <= radius && brushType == 0) || (tot <= radius && texEnable == false))
		{
			float4 white = float4(1.0f, 1.0f, 1.0f, 1.0f);
			white.a = 0.5f;
			ret = white.a * white + (1.0f - white.a) * ret;
		}
	}

	if(abs(pInput.Tex3.x) % 33.333333125 < 0.1 || abs(pInput.Tex3.z) % 33.333333125 < 0.1)
		ret = float4(1, 0, 0, 1);
		
	if(abs(pInput.Tex3.y) % hlDist < 0.5 && drawHL)
	{
		ret.a = 1;
		ret.r = ret.g = ret.b = 0;
	}
	
	return ret;
}


technique EntryPoint
{
	pass p1
	{
		VertexShader = compile vs_3_0 VShader();
		PixelShader = compile ps_3_0 MyShader();
	}
}
